package com.ruoyi.learn.java.design.pattern.behavior;

import java.util.HashMap;
import java.util.Map;

// 1. 上下文 (Context)
// 用于存储变量名到布尔值的映射
class Context {
    private Map<String, Boolean> variableValues = new HashMap<>();

    public void setVariable(String name, boolean value) {
        variableValues.put(name, value);
    }

    public boolean getVariable(String name) {
        Boolean value = variableValues.get(name);
        if (value == null) {
            throw new IllegalArgumentException("Variable '" + name + "' not found in context.");
        }
        return value;
    }
}

// 2. 抽象表达式 (AbstractExpression)
// 声明解释操作
interface Expression {
    boolean interpret(Context context);
}

// 3. 终结符表达式 (TerminalExpression)
// 解释变量
class VariableExpression implements Expression {
    private String name;

    public VariableExpression(String name) {
        this.name = name;
    }

    @Override
    public boolean interpret(Context context) {
        System.out.println("Interpreting variable: " + name);
        return context.getVariable(name);
    }
}

// 4. 非终结符表达式 (NonterminalExpression)
// 解释 NOT 操作
class NotExpression implements Expression {
    private Expression expression;

    public NotExpression(Expression expression) {
        this.expression = expression;
    }

    @Override
    public boolean interpret(Context context) {
        System.out.println("Interpreting NOT expression");
        return !expression.interpret(context);
    }
}

// 4. 非终结符表达式 (NonterminalExpression)
// 解释 AND 操作
class AndExpression implements Expression {
    private Expression left;
    private Expression right;

    public AndExpression(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public boolean interpret(Context context) {
        System.out.println("Interpreting AND expression");
        return left.interpret(context) && right.interpret(context);
    }
}

// 4. 非终结符表达式 (NonterminalExpression)
// 解释 OR 操作
class OrExpression implements Expression {
    private Expression left;
    private Expression right;

    public OrExpression(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public boolean interpret(Context context) {
        System.out.println("Interpreting OR expression");
        return left.interpret(context) || right.interpret(context);
    }
}

// 5. 客户端 (Client)
public class InterpreterPatternDemo {
    public static void main(String[] args) {
        // 创建上下文并设置变量值
        Context context = new Context();
        context.setVariable("A", true);
        context.setVariable("B", false);
        context.setVariable("C", true);

        // 构建抽象语法树 (AST)
        // 表达式: (A AND (NOT B)) OR C
        // AST 结构:
        //           OR
        //          /  \
        //        AND   C
        //       /   \
        //      A    NOT
        //            \
        //             B

        Expression a = new VariableExpression("A");
        Expression b = new VariableExpression("B");
        Expression c = new VariableExpression("C");
        Expression notB = new NotExpression(b);
        Expression aAndNotB = new AndExpression(a, notB);
        Expression finalExpression = new OrExpression(aAndNotB, c);

        // 执行解释
        boolean result = finalExpression.interpret(context);
        System.out.println("Result of expression '(A AND (NOT B)) OR C': " + result);
        // 预期结果: (true AND (NOT false)) OR true = (true AND true) OR true = true OR true = true
    }
}